package com.blog.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.blog.blog.common.Cache;
import com.blog.blog.common.LogAnnotation;
import com.blog.blog.dao.pojo.Article;
import com.blog.blog.service.ArticleService;
import com.blog.blog.vo.ArticleVo;
import com.blog.blog.vo.Result;
import com.blog.blog.vo.WebResult;
import com.blog.blog.vo.params.ArticleParam;
import com.blog.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import java.util.LinkedHashMap;

//使用json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    //@LogAnnotation()
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 最新文章
     * @return
     */
    @PostMapping("/new")
    public Result newArticle() {
        int limit = 5;
        return articleService.newArticle(limit);
    }

    /**
     * 文章归档
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives() {
        return articleService.listArchives();
    }

    /**
     * 文章详情
     */
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long id) {
        ArticleVo articleVo = articleService.findArticleById(id);
        return Result.success(articleVo);
    }

    /**
     * 发布文章
     * @param articleParam
     * @return
     */
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.publish(articleParam);
    }

    /**
     * 个人信息页查询所有文章
     */
    @GetMapping("findAllPersonalArticle")
    public WebResult<List<JSONObject>> findAllPersonalArticle() {
        return WebResult.<List<JSONObject>>builder()
                .code(200)
                .data(articleService.findAllPersonalArticle())
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }

    /**
     * 删除文章接口
     */
    @DeleteMapping("delArticleById")
    public void delArticleById(@RequestParam Long articleId) {
        articleService.delArticleById(articleId);
    }

}
